package com.frogs.ucenter.service.impl;

import java.util.Arrays;

import org.apache.commons.lang.ArrayUtils;
import org.jsoup.Connection.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.system.util.ApplicablePlatformEnum;
import com.frogs.system.util.SmsPartnerEnum;
import com.frogs.system.web.api.entity.ApiUrl;
import com.frogs.system.web.api.response.Response.ApiResultEnum;
import com.frogs.system.web.api.response.Response.ResultParamEnum;
import com.frogs.ucenter.entity.UserMessageSmsVerifyCode;
import com.frogs.ucenter.service.AbsService;
import com.frogs.ucenter.service.SmsService;

@Service("smsService")
public class SmsServiceImpl extends AbsService implements SmsService{
	
	private static Logger log = LoggerFactory.getLogger(SmsServiceImpl.class);
	
	/**
	 * 发送手机验证码 
	 */
	@Override
	public Boolean sendVerifyByphone(String phone, Integer operation, Integer captchaValid) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("SEND_SMS_VERIFY");
		String[] otherParams = {
				ApiUrlParamEnum.phoneNumber.getParamName(), phone, 
				ApiUrlParamEnum.applicablePlatformId.getParamName(),String.valueOf(ApplicablePlatformEnum.WEB.getId()),
				ApiUrlParamEnum.operation.getParamName(), operation.toString(),
				ApiUrlParamEnum.captchaValid.getParamName(), captchaValid.toString()};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() == result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				return Boolean.TRUE;
		} catch (Exception e) {
			log.error("func[sendVerifyByphone] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return Boolean.FALSE;
	}
	
	/**
	 * 检查验证码
	 */
	@Override
	public Boolean checkVerifyCode(String phone, String code, Integer operation) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("CHECK_SMS_VERIFY");
		String[] otherParams = {
				ApiUrlParamEnum.phoneNumber.getParamName(), phone, 
				ApiUrlParamEnum.applicablePlatformId.getParamName(),String.valueOf(ApplicablePlatformEnum.WEB.getId()),
				ApiUrlParamEnum.verificationCode.getParamName(), code,
				ApiUrlParamEnum.operation.getParamName(), operation.toString()};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() == result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				return Boolean.TRUE;
		} catch (Exception e) {
			log.error("func[checkVerifyCode] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return Boolean.FALSE;
	}
	
	/**
	 * 发送文本信息
	 */
	@Override
	public Boolean sendSmsContent(String phone, String messageContent) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("SMS_SEND");
		String[] otherParams = {
				ApiUrlParamEnum.phoneNumber.getParamName(), phone, 
				ApiUrlParamEnum.isReport.getParamName(),String.valueOf(0),
				ApiUrlParamEnum.sendLevel.getParamName(), String.valueOf(9), 
				ApiUrlParamEnum.messageContent.getParamName(), messageContent,
				ApiUrlParamEnum.partner.getParamName(), SmsPartnerEnum.BAI_FEN.getCode()};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() == result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				return Boolean.TRUE;
		} catch (Exception e) {
			log.error("func[checkVerifyCode] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return Boolean.FALSE;
	}
	
	/**
	 * 发送第三方注册时的手机验证码
	 */
	@Override
	public Boolean sendOpenPlatformVerify(String phone, Integer openPlatformId, int lifeCycle) {
		
		if (sendVerifyByphone(phone, UserMessageSmsVerifyCode.SendOperationEnum.OPEN_PLATFORM_REGISTER.getValue(), lifeCycle)) {
			return Boolean.TRUE;
		} else {
			return Boolean.FALSE;
		}
	}
}
